package com.atguigu.crowd.service.impl;

import com.atguigu.crowd.entity.Auth;
import com.atguigu.crowd.entity.AuthExample;
import com.atguigu.crowd.mapper.AuthMapper;
import com.atguigu.crowd.service.api.AuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class AuthServiceImpl implements AuthService {

    @Autowired
    private AuthMapper authMapper;

    @Override
    public List<Auth> getAll() {
        return authMapper.selectByExample(new AuthExample());
    }

    @Override
    public List<Integer> getAssignedAuthIdByRoleId(Integer roleId) {
        return authMapper.selectAssignedAuthIdByRoleId(roleId);
    }

    @Override
    public void saveRoleAuthRelationship(Map<String, List<Integer>> map) {
        //1.获取roleId
        List<Integer> roleIdList=map.get("roleId");
        Integer roleId=roleIdList.get(0);

        //2.删除旧的关联关系数据
        authMapper.deleteOldRelationship(roleId);

        //3.获取authIdList
        List<Integer> authIdList=map.get("authIdArray");

        //4.判断 authIdList 是否有效 有效则插入数据
        if(authIdList !=null && authIdList.size() >0){
            //注意这里不能是authIdList !=null || authIdList.size() >0
            //这里是集合 即便没有值也会是[]这种形式（有水杯 水杯中没水） 并不是null（空 没有水杯）
            // 所以没值时 authIdList !=null是正确的 会造成短路 且会执行插入语句
            //但确实没值 则sql语句错误 会报错
            authMapper.insertNewRelationship(roleId,authIdList);
        }
    }

    @Override
    public List<String> getAssignedAuthNameByAdminId(Integer adminId) {
        return authMapper.selectAssignedAuthNameByAdminId(adminId);
    }
}
